package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.NewDao;
import model.New;

/**
 * Servlet implementation class NewServlet
 */
@WebServlet("/NewServlet")
public class NewServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out =  response.getWriter();
		String flag = request.getParameter("handle");
		System.out.println(flag);
		if (flag != null) {
			if(flag.equals("delNew")) {
				String nid = request.getParameter("nid");
				System.out.println(nid);
				New new1 = new New();
				try {
					new1 = NewDao.getNewNoCache(Integer.parseInt(nid));
					System.out.println(new1.getInfo());
					if(NewDao.delNew(new1)) {
						out.print("<script>alert('删除成功');window.location.href='admin/AdminNewList.jsp'</script>");
					}else{
						out.print("<script>alert('删除失败');window.location.href='admin/AdminNewList.jsp'</script>");
					};
				} catch (Exception e) {
					out.print("<script>alert('删除失败');window.location.href='admin/AdminNewList.jsp'</script>");
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			if(flag.equals("updateNew")) {
				//接收数据
				String nid = request.getParameter("nid");
				String name = request.getParameter("name");
				String info = request.getParameter("info");
				//创建新对象
				New new1 = new New();
				new1.setId(Integer.parseInt(nid));
				new1.setInfo(info);
				new1.setName(name);
				try {
					//进行更新
					NewDao.updateNew(new1);
					out.print("<script>alert('修改成功');window.location.href='admin/AdminNewList.jsp'</script>");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					out.print("<script>alert('修改失败');window.location.href='admin/AdminNewList.jsp'</script>");
				}
			}
			if(flag.equals("addNew")) {
				String name = request.getParameter("name");
				String info = request.getParameter("info");
				System.out.println(name);
				System.out.println(info);
				New new1 = new New();
				new1.setInfo(info);
				new1.setName(name);
				try {
					NewDao.addNew(new1);
					out.print("<script>alert('添加成功');window.location.href='admin/AdminNewList.jsp'</script>");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					out.print("<script>alert('添加失败');window.location.href='admin/AdminNewList.jsp'</script>");
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
